//
//  PlayerViewCell.m
//  ZYTS-iPhone
//
//  Created by yuzhou on 13-1-29.
//  Copyright (c) 2013年 wzyk. All rights reserved.
//

#import "PlayerViewCell.h"

@implementation PlayerViewCell

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        // Initialization code
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playingIndexDidChange) name:kPlayerIndexDidChange object:nil];
    }
    return self;
}

- (void)updateDisplay
{
    indexLabel.text = [NSString stringWithFormat:@"%03d",_index+1];
    titleLabel.text = [NSString stringWithFormat:@"%@",[_info valueForKey:@"sub_name"]];
    
    if (self.index%2 == 0) {
        self.contentView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1];
    }
    else {
        self.contentView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.1];
    }
    
    [self playingIndexDidChange];
}

- (void)playingIndexDidChange
{
    playingImageView.hidden = !(_index == [[AudioPlayer defaultPlayer] playingIndex]);
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    [super setHighlighted:highlighted animated:animated];
    
    [UIView animateWithDuration:animated?0.5:0 animations:^{
        if (highlighted) {
            self.contentView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
        }
        else {
            if (self.index % 2 == 0) {
                self.contentView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1];
            }
            else {
                self.contentView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.1];
            }
        }
    }];
}

- (void)dealloc
{
    [_info release];
    [indexLabel release];
    [titleLabel release];
    [timeLabel release];
    [playingImageView release];
    [super dealloc];
}

@end
